package demo1;

import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
public class Test {
    //数组中两个字符串间的最短距离
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String[] strings = new String[n];
        String str1 = scanner.nextLine();
        String str2 = scanner.nextLine();
        for (int i = 0; i < n; i++) {
            strings[i] = scanner.nextLine();
        }
        int prev1 = -1;
        int prev2 = -1;
        int ret = -1;
        for (int i = 0; i < strings.length; i++) {
            if(strings[i].equals(str1)){
                prev1 = i;
                if(prev2!=-1){
                    ret = ret==-1?prev1-prev2:Math.min(ret,prev1-prev2);
                }
            }else if(strings[i].equals(str2)){
                prev2 = i;
                if(prev1!=-1){
                    ret = ret==-1?prev2-prev1:Math.min(ret,prev2-prev1);
                }
            }
        }
        System.out.println(ret);
    }

    //最小花费爬楼梯
    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int[] arr = new int[num+2];
        int[] dp = new int[arr.length];
        for (int i = 0; i < num; i++) {
            arr[i] = scanner.nextInt();
        }
        for (int i = 2; i <=num ; i++) {
            dp[i] = Math.min(dp[i-1]+arr[i-1],dp[i-2]+arr[i-2]);
        }
        System.out.println(dp[arr.length]);
//        for (int i = 0; i < num; i++) {
//            int a = Math.min(arr[i]+arr[i+1],arr[i]+arr[i+2]);
//            int b = Math.min(arr[i+1]+arr[i+2],arr[i+1]+arr[i+3]);
//            if(a<b){
//                count+=arr[i];
//            }else if(b<a){
//                i++;
//                count+=arr[i];
//            }else {
//                i++;
//                count+=arr[i];
//            }
//        }
//        System.out.println(count);
    }

    //牛牛的快递
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        float wight = scanner.nextFloat();
        String flag = scanner.nextLine();
        int price = 0;
        if(wight>1){
            wight-=1;
            price = 20;
        }
        if(flag.charAt(0)=='y'){
            price+=5;
        }
        while(wight>0){
            price++;
            wight-=1;
        }
        System.out.println(price);
    }
}
